<?php 
class AdCat extends AdType
{
	public $ParentId;
	public $LinkToTypeId;
	public $LinkToCatId;
	
	public function AdCat($values = '')
	{ 
		self::setDataTable('am_categories');
		if(is_array($values))
		{ self::setValues($values); }
	}
	
	public function __toString()
	{
		$out  = '';
		$out .= self::getId();
		$out .= self::getTitle();
		return $out;
	}
	
	public function getParentId() { return intval($this->ParentId); }
	public function getLinkToTypeId() { return intval($this->LinkToTypeId); }
	public function getLinkToCatId() { return intval($this->LinkToCatId); }
	//public function getImageURL() { return ADCAT_URL.self::getImage(); }
	public function getImageURL($w=0, $h=0) 
	{ 
		if(($w > 0) || ($h > 0))
		{ return ADCAT_URL.self::getImage(); }
		else
		{ 
			if(is_object(self::getImageObject()))
			{ return self::getImageObject()->getLink($w, $h); }
		}
	}
	public function getImageThumbURL() { return ADCAT_URL.'thumbs/'.self::getImage(); }
	
	public function getLink()
	{
		if(self::getLinkToTypeId() > 0)
		{ $link = '/adtype/'.self::getLinkToTypeId().'/'; }
		elseif(self::getLinkToCatId() > 0)
		{ $link = '/adcat/'.self::getLinkToCatId().'/'; }
		else
		{ $link = '/adcat/'.self::getId().'/'; }
		
		if(intval($_GET['bt']) > 0)
		{ $link .= '?bt='.intval($_GET['bt']); }
		return $link;
	}
	
	public function setParentId($val) { $this->ParentId=intval($val); }
	
	function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.self::getDataTable().' SET ';
			$sql .= '`parentId`='.self::getParentId().', ';
			$sql .= '`typeId`='.self::getTypeId().', ';
			$sql .= '`LinkToTypeId`='.self::getLinkToTypeId().', ';
			$sql .= '`formId`='.self::getFormId().', ';
			$sql .= '`formBuyId`='.self::getFormBuyId().', ';
			$sql .= '`title`="'.self::formatText(self::getTitle(1)).'", ';
			$sql .= '`titleEn`="'.self::formatText(self::getTitleEn()).'", ';
			$sql .= '`description`="'.self::formatHTML(self::getDescription()).'", ';
			$sql .= '`image`="'.self::formatText(self::getImage()).'", ';
			$sql .= '`active`='.self::getActive().' ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
			mysql_query($sql);
		}
	}
	
	function uploadImage($file)
	{
		global $thumb;
		self::setImage(self::uploadObjectImage(ADCAT_PATH, $file, $thumb['ADCAT']));
	}
	
	function deleteImage()
	{
		global $thumb;
		self::setImage(self::deleteObjectImage(ADCAT_PATH, self::getImage(), $thumb['ADCAT']));
	}
}
?>